//
//  UIView+ZMAnimation.m
//  ZMKitDemo
//
//  Created by zm on 2018/6/13.
//  Copyright © 2018年 zm. All rights reserved.
//

#import "UIView+ZMAnimation.h"

@implementation UIView (ZMAnimation)

#pragma mark- Animaion

- (void)zm_runAddAnimaion {
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
        [self.layer setValue:@1.7 forKeyPath:@"transform.scale"];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
            [self.layer setValue:@0.9 forKeyPath:@"transform.scale"];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
                [self.layer setValue:@1 forKeyPath:@"transform.scale"];
            } completion:^(BOOL finished) {
                
            }];
        }];
    }];
}

- (void)zm_runRemoveAnimaion {
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
        [self.layer setValue:@0.8 forKeyPath:@"transform.scale"];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
            [self.layer setValue:@1 forKeyPath:@"transform.scale"];
        } completion:^(BOOL finished) {
            
        }];
    }];
}

- (void)zm_keyframeAnimation {
    CGPoint pt = self.layer.position;
    CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGPoint pt1 = CGPointMake(pt.x + 20, pt.y);
    CGPoint pt2 = CGPointMake(pt.x - 20, pt.y);
    CGPoint pt3 = CGPointMake(pt.x + 10, pt.y);
    CGPoint pt4 = CGPointMake(pt.x - 10, pt.y);
    
    keyframe.values = @[[NSValue valueWithCGPoint:pt],
                        [NSValue valueWithCGPoint:pt1],
                        [NSValue valueWithCGPoint:pt2],
                        [NSValue valueWithCGPoint:pt3],
                        [NSValue valueWithCGPoint:pt4],
                        [NSValue valueWithCGPoint:pt]];
    
    keyframe.keyTimes = @[@(0.0),@(0.2),@(0.4),@(0.6),@(0.8),@(1.0)];
    keyframe.duration = 0.5;
    [self.layer addAnimation:keyframe forKey:nil];
}


@end
